alter proc spThemCTXuatKho (@maXuatKho int, @maNL int, @soLuong float, @maKho int)
as
begin
	Insert Into CTXuatKho
	(
		MaXuatKho,
		MaNL,
		SoLuong
	)
	Values
	(
		@maXuatKho,
		@maNL,
		@soLuong
	)
	DECLARE @luongTon float
	DECLARE @toiThieu float
	DECLARE @tinhTrang int

	select @luongTon = LuongTon, @toiThieu = SucChuaThapNhat
	from ChiTietKho
	where MaKho = @maKho and MaNL = @maNL

	set @luongTon = @luongTon - @soLuong
	if (@luongTon > @toiThieu)
		set @tinhTrang = 2
	else if (@luongTon = 0)
		set @tinhTrang = 0
	else set @tinhTrang = 1

	update ChiTietKho
	Set
			LuongTon = @luongTon,
			TinhTrang = @tinhTrang
		Where MaKho = @maKho and MaNL = @maNL
end